package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.entity.Teachplan;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j

@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {
    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private TeachplanService teachplanService;

    /**
     * 根据课程id获取教师信息集合
     *
     * @param courseBaseId 课程id
     * @return 教师信息集合
     * ==========业务分析========
     * 1.判断关键数据
     * courseBaseId,companyId
     * 2.查询对应的集合,判断业务数据
     * 集合中  courseId,photograph,position,teacherName不能为空
     * 3.将结果封装并返回
     */
    @Override
    public List<CourseTeacherDTO> queryTeacherList(Long courseBaseId) {
        //1.判断关键数据
        if (ObjectUtils.isEmpty(courseBaseId)) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //判断课程是否存在
        CourseBase courseBase = this.courseBaseService.getById(courseBaseId);
        if (ObjectUtils.isEmpty(courseBase)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        //判断课程是否删除
        if (courseBase.getStatus().equals(CommonEnum.DELETE_FLAG.getCodeInt())) {
            //如果课程已删除，就报异常
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        //查询对应集合,判断业务数据
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件
        queryWrapper.isNotNull(CourseTeacher::getPhotograph);
        queryWrapper.isNotNull(CourseTeacher::getPosition);
        queryWrapper.isNotNull(CourseTeacher::getTeacherName);
        queryWrapper.eq(CourseTeacher::getCourseId, courseBaseId);
        List<CourseTeacher> teacherList = this.list(queryWrapper);
        if (ObjectUtils.isEmpty(teacherList)) {
            return new ArrayList<CourseTeacherDTO>();
        }
        //将po转为dto集合
        List<CourseTeacherDTO> resultList = CourseTeacherConvert.INSTANCE.entitys2dtos(teacherList);

        return resultList;
    }

    /**
     * 创建或修改教师信息
     *
     * @param dto 教师信息
     * @return 更新之后的教师信息
     * <p>
     * =======业务分析=====
     * 1.判断关键数据
     * 2.根据courseTeachId主键来判断是修改还是创建
     * 3.执行相应操作
     */
    @Override
    @Transactional
    public CourseTeacherDTO createOrModifyTeacher(CourseTeacherDTO dto) {
        //判断关键数据
        if (ObjectUtils.isEmpty(dto)) {
            //课程教师信息不存在
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        //判断课程id
        if (ObjectUtils.isEmpty(dto.getCourseId())) {
            //修改数据的ID值不能为空
            ExceptionCast.cast(ContentErrorCode.E_120009);
        }
        //判断教师信息图片
        if (ObjectUtils.isEmpty(dto.getPhotograph())) {
            //课程教师图片不能为空
            ExceptionCast.cast(ContentErrorCode.E_1200504);
        }
        //判断教师职位
        if (ObjectUtils.isEmpty(dto.getPosition())) {
            ExceptionCast.cast(ContentErrorCode.E_1200505);
        }
        //判断教师姓名
        if (ObjectUtils.isEmpty(dto.getTeacherName())) {
            ExceptionCast.cast(ContentErrorCode.E_1200506);
        }
        //获取教师信息id
        Long teachId = dto.getCourseTeacherId();
        //将dto转换为po
        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dto2entity(dto);
        CourseTeacherDTO result = null;
//        据courseTeachId主键来判断是修改还是创建,因为在数据库中已经存在数据时才会生成此主键
        if (ObjectUtils.isEmpty(teachId)) {
            result = createCourseTeacher(courseTeacher);
        } else {
            result = modifyCourseTeacher(courseTeacher);
        }
        return result;
    }

    //修改教师信息
    private CourseTeacherDTO modifyCourseTeacher(CourseTeacher courseTeacher) {

        boolean result = this.updateById(courseTeacher);
        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }
        CourseTeacherDTO courseTeacherDTO = CourseTeacherConvert.INSTANCE.entity2dto(courseTeacher);
        return courseTeacherDTO;
    }

    //创建教师信息
    private CourseTeacherDTO createCourseTeacher(CourseTeacher courseTeacher) {
        boolean result = this.save(courseTeacher);
        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }
        CourseTeacherDTO courseTeacherDTO = CourseTeacherConvert.INSTANCE.entity2dto(courseTeacher);
        return courseTeacherDTO;
    }


    /**
     * 根据课程id和教师信息id删除教师信息
     *
     * @param courseBaseId    课程id
     * @param courseTeacherId 教师信息id
     *                        =======业务分析=====
     *                        1.判断关键数据
     *                        courseBaseId,courseTeacherId
     *                        2.执行业务操作删除,此操作为物理删除
     */
    @Override
    @Transactional
    public void deleteTeacher(Long courseBaseId, Long courseTeacherId) {
        //1.判断关键数据
        if (ObjectUtils.isEmpty(courseBaseId) || ObjectUtils.isEmpty(courseTeacherId)) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getCourseId, courseBaseId);
        int count = this.teachplanService.count(wrapper);
        if (count < 1) {
            ExceptionCast.cast(ContentErrorCode.E_120402);
        }

        //查询对应集合,判断业务数据
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件
        queryWrapper.eq(CourseTeacher::getCourseId, courseBaseId);
        queryWrapper.eq(CourseTeacher::getId, courseTeacherId);
        //2.执行业务操作
        boolean result = this.remove(queryWrapper);
        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }
    }
}
